package com.ycw.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.ycw.dao.FlatplayDao;
import com.ycw.pojo.Flatplay;
import com.ycw.pojo.pageIndex;
import com.ycw.util.DBUtil;
import com.ycw.util.MyBatisUtil;

public class FlatplayImpl extends DBUtil implements FlatplayDao {
	MyBatisUtil myBatisUtil = new MyBatisUtil();
	SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
	SqlSession sqlSession = sqlSessionFactory.openSession();

	// 查询公寓活动总个数===================================
	@Override
	public int count() {

		int i = sqlSession.getMapper(FlatplayDao.class).count();
		return i;

	}

	// 分页查询所有公寓活动===================================
	@Override
	public List<Flatplay> getAllFlatplay(pageIndex pageIndex) {

		// 获取数据总数
		int sum = count();
		List<Flatplay> flatplays = sqlSession.getMapper(FlatplayDao.class).getAllFlatplay(pageIndex);

		flatplays.get(0).setSum(sum);

		sqlSession.close();
		return flatplays;

	}

	// 通过id查看详情===================================
	@Override
	public List<Flatplay> getFlatplayDetail(Flatplay flatplay) {

		List<Flatplay> list = sqlSession.getMapper(FlatplayDao.class).getFlatplayDetail(flatplay);
		sqlSession.close();
		return list;
	}

	// 添加公寓活动信息
	@Override
	public int addFlatplayInfo(Flatplay flatplay) {

		int i = 0;
		try {
			i = sqlSession.getMapper(FlatplayDao.class).addFlatplayInfo(flatplay);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}

		return i;
	}

	// 根据id删除某个公寓活动信息===================================
	@Override
	public int deleteFlatplayInfo(Flatplay flatplay) {

		int i = 0;
		try {
			i = sqlSession.getMapper(FlatplayDao.class).deleteFlatplayInfo(flatplay);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
		return i;
	}

	// 根据id修改某个公寓活动信息===================================
	@Override
	public int updateFlatplayInfo(Flatplay flatplay) {
		System.out.println(flatplay.toString());
		int i = 0;
		try {
			i = sqlSession.getMapper(FlatplayDao.class).updateFlatplayInfo(flatplay);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
		return i;

	}

	// 条件查询查总数===================================
	@Override
	public int getSumByCondition(Flatplay flatplay) {

		int sum = sqlSession.getMapper(FlatplayDao.class).getSumByCondition(flatplay);
		return sum;

	}

	// 根据条件分页查询===================================
	@Override
	public List<Flatplay> getFlatplayByCondition(Flatplay flatplay) {
		int sum = getSumByCondition(flatplay);

		List<Flatplay> flatplays = sqlSession.getMapper(FlatplayDao.class).getFlatplayByCondition(flatplay);
		flatplays.get(0).setSum(sum);
		return flatplays;
	}

}
